package com.itbomb.tvdemo;

import android.content.res.Resources;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

/**
 * @author A.D.Wilme
 * @date on 2018/8/27  15:47
 * @email A.D.Wilme@gmail.com
 * @describe TODO
 */
public class FancyCoverFlowSampleAdapter extends BaseAdapter {
    private String[] imgUrls;

    public FancyCoverFlowSampleAdapter(String[] imgUrls) {
        this.imgUrls = imgUrls;
    }

    @Override
    public int getCount() {
        return imgUrls.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SaturationImageView imageView;
        if (convertView != null) {
            imageView = (SaturationImageView) convertView;
        } else {
            imageView = new SaturationImageView(parent.getContext());
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageView.setLayoutParams(new Gallery3DView.LayoutParams(600, 800));
        }

        Glide.with(parent.getContext())
                .asBitmap()
                .load(imgUrls[position])
                .apply(RequestOptions.bitmapTransform(new ReflectedRoundedCorners(dip2px(8), Color.parseColor("#1A1438")))
                        .diskCacheStrategy(DiskCacheStrategy.NONE) //为了方便测试，取消缓存
                        .skipMemoryCache(true))
                .into(imageView);
        return imageView;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    private int dip2px(float value) {
        return (int) (value * Resources.getSystem().getDisplayMetrics().density + 0.5f);
    }
}
